package apis

import (
	"gitee.com/sansaniot/ssiot-core/httpmvc/api"
	"gitee.com/sansaniot/ssiot-core/httpmvc/jwtauth/user"
	"gitee.com/sansaniot/ssiot-core/serrors"
	"github.com/gin-gonic/gin"
	global "ssadmin/common/constant"
	"ssadmin/common/utils"
	"ssadmin/internal/admin/models/command"
	"ssadmin/internal/admin/models/query"
	"ssadmin/internal/admin/service"
)

type Dept struct {
	api.Api
}

func (e Dept) FindList(c *gin.Context) {
	e._getDeptList(c, false)
}

func (e Dept) FindListAll(c *gin.Context) {
	req := query.DeptQuery{}
	s := service.Dept{}

	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	//获取用户机构列表
	uid := user.Get(c, global.CacheKeyUserId).(string)
	userName := user.Get(c, global.CacheKeyUserName).(string)
	req.CreatorId = uid
	req.UserId = uid
	req.UserName = userName

	//获取机构
	dataMap := make([]map[string]interface{}, 0)
	err = s.GetList(&req, &dataMap)
	e.Data(dataMap)
}

// FindListLazy 超过1000个分组，使用异步加载
func (e Dept) FindListLazy(c *gin.Context) {
	e._getDeptList(c, true)
}

func (e Dept) _getDeptList(c *gin.Context, lazy bool) {
	req := query.DeptQuery{}
	s := service.Dept{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}
	//获取用户机构列表
	uid := user.Get(c, global.CacheKeyUserId).(string)
	userName := user.Get(c, global.CacheKeyUserName).(string)
	req.CreatorId = uid
	req.UserId = uid
	req.UserName = userName

	//获取机构
	dataMap := make([]map[string]interface{}, 0)
	err = s.GetList(&req, &dataMap)

	//转换成菜单树
	resData := make(map[string]interface{})
	root := make(map[string]interface{})
	tree := make([]map[string]interface{}, 0)
	root["total"] = len(dataMap)
	root["hasChildren"] = true
	root["hasParent"] = false
	root["id"] = req.ParentId
	root["parentId"] = ""
	root["text"] = req.DeptName
	if lazy && len(dataMap) > 1000 {
		tree = utils.GetRootList(dataMap, "id")
	} else {
		tree = utils.ConvrtListToTree(dataMap, "id")

	}
	root["children"] = tree
	resData["records"] = root
	resData["lazy"] = lazy
	e.Data(resData)
}

// GetDetail 获取Dept
func (e Dept) GetDetail(c *gin.Context) {
	req := query.DeptQuery{}
	s := service.Dept{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}
	req.CreatorId = user.Get(c, global.CacheKeyUserId).(string)
	data := make(map[string]interface{})
	err = s.GetDetail(&req, data)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}
	e.Data(data)
}

// Insert 创建Dept
func (e Dept) Insert(c *gin.Context) {
	req := command.DeptInsertReq{}
	s := service.Dept{}

	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors

	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	req.Bind(c)
	err = s.Insert(&req)
	if err != nil {
		e.Fail(serrors.InsertFail)
		return
	}

	e.Data(req.DeptId)
}

// Update 修改Dept
func (e Dept) Update(c *gin.Context) {
	req := command.DeptUpdateReq{}
	s := service.Dept{}

	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors

	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.InvalidParam)
		return
	}

	req.UpperCreatorId = user.Get(c, global.CacheKeyCreatorId).(string)
	req.CreatorId = user.Get(c, global.CacheKeyUserId).(string)
	req.Creator = user.Get(c, global.CacheKeyUserName).(string)
	err = s.Update(&req)

	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.UpdateFail)
		return
	}

	e.Data(req.GetId())
}

// Delete 删除Dept
func (e Dept) Delete(c *gin.Context) {
	s := service.Dept{}
	req := command.DeptDeleteReq{}

	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors

	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.InvalidParam)
		return
	}

	req.CreatorId = user.Get(c, global.CacheKeyUserId).(string)
	err = s.Remove(&req)

	if err != nil {
		e.Fail(serrors.Fail)
		return
	}

	e.Data(req.GetId())
}
